Tangible Software Solutions
Java to C# Converter Help
Contents
- Folder Conversion
- File & Snippet Conversion
- Command Line Conversion
- Code Formatting Options
- Conversion Message Options
- Custom String Replacements
- Type & Member Replacements
- Diagnostic Options
- File Encoding Options
- Additions & Omissions
- Naming Conventions
- Java Class Library to .NET Options
- Omit Types from Conversion
- Accessor & Mutator Method Conversion
- Miscellaneous Options
Folder Conversion
The Folder Converter tab allows you to convert all Java code
within a folder. Once the files are converted, you can then view Java to C# Converter
comments, compare converted code files to original code files, and view the converted
code folder.
The Conversion History listview displays previously converted sources and allows you to
view remaining Java to C# Converter comments and view the converted code folder.
File & Snippet Conversion
The File & Snippet Converter tab allows you to convert
Java code snippets or files. From the left side textbox, you can type
or paste code snippets, insert files, or view Java to C# Converter sample snippets.
Once a file or snippet is converted, you can select and copy the results, save the
results to a file, or search the results.
Command Line Conversion
Java to C# Converter can be launched from the command line
with two or three parameters. If you specify folders, the first parameter is the folder
containing the .java files that you wish to convert and the second parameter is the folder
that the converted files will be written to. If you specify files, the first parameter
is the source code file that you wish to convert and the second parameter is the name
of the converted C# file. If a third parameter is supplied, then it is interpreted as
the location of a conversion options file to import before the conversion starts. A conversion options file
has the extension '.opt' and is produced by exporting existing options to an xml file from the converter.
e.g. (folder conversion),
"Java to C# Converter" "C:\Java Source Files" "C:\C# Converted Files"
e.g. (file conversion),
"Java to C# Converter" "C:\Java Source Files\testfile.java" "C:\C# Converted Files\testfile.cs"
e.g. (folder conversion with a conversion options file specified),
"Java to C# Converter" "C:\Java Source Files" "C:\C# Converted Files" "C:\Options\Java to C# Converter.opt"
Once the command line conversion is complete, Java to C# Converter will shut down.
Note that Java to C# Converter must be in double quotes since the executable name
contains spaces.
- Braces: You can choose whether to place opening
braces on the next line after the construct header (default) or at the end of the same line.
You can choose whether to have braces vertically aligned with the construct (default) or
indented. Loops and if or else blocks with a single statement are converted by default
with enclosing braces. If you prefer to exclude braces for these cases then check
the preference to remove braces for single statement loops and if/else blocks.
- Indentation: You can choose whether the converted code uses
tabs or spaces for the leading white space. If you choose tabs for leading white space, then
leading spaces in the original code of the length that you specify are replaced by tabs. If you
choose spaces for leading white space, then tabs in the original code are replaced by spaces
of the length that you specify.
- Spacing: You can choose the degree to which
Java to C# Converter inserts spaces in the converted code. By default, binary operators
are padded with spaces and control structures have a space inserted after the keyword,
but you can switch off this behavior or insert spaces in other situations.
- Control Statements: If you
select this option, the converter will break apart control structure statements
(if and while statements) by the main conditions. See the output in the
textbox to see the effect of this option.
- Method Declarations: If you select this
option, the converter will break apart method declarations by the method parameters. See the
output in the textbox to see the effect of this option.
- Method Calls: If you select this
option, the converter will break apart method calls by the method call arguments. See the
output in the textbox to see the effect of this option.
- String Literal Concatenation: If you select this
option, the converter will break apart multiple string literal concatenations to separate
lines. See the output in the textbox to see the effect of this option.
Conversion Message Options
On this options dialog, you can customize the comment prefixes
that the converter uses for messages and also toggle the messages that Java to C# Converter
displays on or off.
You can easily set all or none to display via the 'Select All' and 'Select None' buttons.
Custom String Replacements
Java to C# Converter allows you to specify your own custom
replacements in the converted code. This can be useful for cases where you wish to
specify a replacement for a Java library data type that isn't handle by
Java to C# Converter.
For each replacement string you can specify the regex option, when to perform the replacement
(pre or post conversion), whether or not to match case, whether to match whole words only,
and whether comments or string literals should be modified. The whole words and string literals
options do not apply if the regex option is selected.
To span mulitple lines, either paste directly into the 'Find' and 'Replacement'
textboxes, or use the escaped characters: \r\n\t.
The "Active" checkbox can be unchecked if you wish to temporarily bypass the replacement.
Replacements are made in the order that they appear on the options dialog. For example,
the second replacement is made to each line of code after the first replacement has already
been made. To change the order of replacements, use the 'Up' and 'Down' buttons.
For an example of the regex option, you could do a custom replacement of "Foo(x)" with "Bar(x)",
where "x" is any argument, by specifying the "Find" field as "Foo(([\sa-zA-Z_$]*))" and the
"Replace" field as "Bar$1". Note that there are differences in various regex engines - the
regex engine used by the converter is .NET System.Text.RegularExpressions.Regex.
Type & Member Replacements
Java to C# Converter allows you to specify your own custom
type and member replacements in the converted code. This can be useful for cases
where you wish to specify replacements for Java library member calls of a specific
type that are not handled by Java to C# Converter.
For each external type, you can specify an optional type replacement and multiple member
replacements. For types, you can specify template/generic types, which may include '?' as
a wildcard type. A wildcard in the replacement type must have a matching wildcard in the
same position in the original type.
e.g., the following will replace all references to type 'Foo' having one 'int' type parameter
with 'Bar', having no type parameters:
original type: Foo<int>
replacement type: Bar
e.g., the following will replace all references to type 'Foo' having one type parameter of any
type with 'Bar', having the same type parameter:
original type: Foo<?>
replacement type: Bar<?>
For each member replacement, you specify the original member name, whether the member is static
or not, and whether the member is a method or a field. You also specify the number of arguments
you expect.
For the replacement member, you specify whether the replacement is static or not, and whether
the replacement is a method or a field. You also specify the new list of arguments and can
add literal arguments. For replacement of static methods with instance members, you also
specify the argument position which corresponds to the object instance for the new instance
member. For replacement of instance members with static members, you specify the new class
name used to call the static member (for static methods, you also specify the position in
the argument list to insert the previous instance qualifier, using the string "instance"
(quotes not included) at the appropriate position in the "new argument list" field).
For example, if you want to replace all instance calls to the 'Foo' type method 'Bar'
where you want the instance qualifier to be the new first argument, you would specify
"instance, a1" (quotes not included) for the "new argument list" field, assuming that there
was one argument in the original method calls.
Diagnostic Options
These options are intended to provide information to
Tangible Software Solutions if a conversion is taking longer than expected.
'Record processing by file' instructs the converter to make a record of the files
processed and the time taken for each process. 'Record the last 100 lines of code
parsed before cancelling conversion' instructs the converter to make a record of the
most recent lines parsed before you cancelled the conversion.
File Encoding Options
The converter detects input file encoding from the byte order
mark (BOM), but if the BOM is missing the converter assumes by default that the file encoding
is UTF-8. You can use this option to specify a different default encoding to use in the absence
of a BOM. Output is written in the same encoding as the encoding detected for the original file,
or your default encoding option if the original coding was not detected via the BOM.
For UTF-8, you can choose whether or not to include the BOM for the output - this is normally
used in Windows environments.
Additions & Omissions
- Add to the top of all converted files: If you add text
for this option, that text will be added to the top of every converted file. This option
does not apply to code snippet conversions.
- Omit code from conversion wrapped in the following xml tag comments:
If you would like to have code omitted from the conversion output instead of converted,
then enter a tag that you will use to identify this code to Java to C# Converter. For
example, if you enter the tag "code", then conversion of the code inside
of a <code>...</code> comment pair will be omitted by the converter.
- Comment code wrapped in the following xml tag comments:
If you would like to have Java code left unconverted and commented out in the conversion
output, then enter a tag that you will use to identify this code to Java to C# Converter.
For example, if you enter the tag "code", then the code inside of
a <code>...</code> comment pair will be commented out by the converter.
- Uncomment code in comments wrapped in the following xml tag - with conversion:
If you would like to have Java code within comments uncommented and converted,
then enter a tag that you will use to identify this code to Java to C# Converter.
For example, if you enter the tag "code", then the text inside
of <code>...</code> blocks within comments will be converted by Java to C# Converter
and uncommented. Note that this code must fit into the context of the surrounding
code since it will be uncommented in the output.
- Uncomment code in comments wrapped in the following xml tag - without conversion:
If you would like to have Java code within comments uncommented without conversion,
then enter a tag that you will use to identify this code to Java to C# Converter.
For example, if you enter the tag "code", then the text inside
of <code>...</code> blocks within comments will be uncommented without conversion
by Java to C# Converter. Note that this code must fit into the context of the
surrounding code since it will be uncommented in the output.
Naming Conventions
By default, the converter does not change naming conventions,
but you can select these options to cause the converter to modify names to conform
to these common C# conventions.
Java Class Library to .NET Options
- Use nullable types for the following structs:
The converter uses structs for a few Java class conversions. By default, the
converter uses the non-nullable version of structs since this is more
common in C#. However, if you want the converter to use the nullable versions
of these structs to better replicate the original Java behavior (since the original Java
type was a class, which allows null), then select these options.
- Use StringComparison.Ordinal for EndsWith, StartsWith, IndexOf, and LastIndexOf:
By default, the converter adds StringComparison.Ordinal to conversions of endsWith,
startsWith, indexOf, and lastIndexOf since this will replicate the Java behavior exactly,
but you have the option of omitting this argument.
- Convert HashMap & Hashtable 'get' to the strict equivalent to handle absence of the key:
By default, the converter translates HashMap & Hashtable get to .NET Dictionary
indexing, but .NET Dictionary indexing throws an exception when the key is not found.
Select this option to convert get to a helper extension method which reproduces
the original Java get behavior exactly.
Omit Types from Conversion
Any type entered here will be omitted from most
conversion logic. You would normally enter library type names here to prevent the
default conversion of objects of those types.
Accessor & Mutator Method Conversion
If you choose this
option, Java parameterless methods starting with get or is and
single-parameter methods starting with set will be converted to C# properties if the
original method name followed the common Java lower camel case naming convention.
If you select 'Convert to automatic properties if possible', then automatic properties are used
provided that the 'get' method is a single line which returns a private backing field,
and the 'set' method is a single line which sets the same backing field.
Miscellaneous Options
- Renaming tag for code element conflicts: Java to C# Converter
must occasionally rename class members to avoid conflicts. You can choose your own
renaming tag and whether to place it at the beginning or end of the original name.
- Convert complex enums to classes:
By default, Java to C# Converter converts enums containing fields or methods to classes
containing an inner enum and methods to reproduce the behavior of Java enums.
- Use nullable enums for instances of enums not converted to classes:
If you choose this option, variables, parameters, and methods of enum types are
converted to C# nullable types, unless the enum was converted to a class.
- Copy non-code files in folder conversions: By default,
the converter does not copy non-code files in folder conversions. If you select this
option, all non-code files are copied to the target folder.
- Promote types to public access: If you choose this
option, all types in the converted code will be marked public. This is to
compensate for Java's failure to enforce coherent type visibility (e.g.,
allowing a public method to have a non-public return type or allowing a
public class to inherit from a non-public class).
- Promote non-private instance methods to public access:
If you choose this option, all non-private instance methods in the converted code will
be marked public. This is to compensate for Java allowing base methods to
have lower visibility than overriding methods.
- Promote nested class private members to internal access:
By default, Java to C# Converter promotes nested class private members to internal,
since Java nested class private members are visible to the outer class, while C# private
members are not visible to the outer class.
- Convert Java functional interfaces to C# delegates:
By default, Java to C# Converter converts functional interfaces to C# delegates
if the functional interface has no default, static, or Object overriding methods.
- Add 'using' statements to minimize namespace references:
By default, Java to C# Converter converts fully qualified type names in Java to fully
qualified type names in C#, but this option reduces namespace references by adding
additional using statements.
- Convert generic type arguments of Java 'wrapper' types (e.g., Integer) to primitive types:
By default, the converter changes generic type arguments to primitive types,
if possible (e.g., ArrayList<Boolean> to List<bool>), but you can unselect
this option to convert to System.Nullable (e.g., ArrayList<Boolean> to
List<bool?>). Although the System.Nullable type is closer to the original
Java wrapper type, the converter uses the primitive type by default since this
is usually intended in the original Java code, but not allowed in Java
(Java generic type arguments cannot be primitive types).
- Convert strings with escaped characters to verbatim strings:
By default, Java to C# Converter maintains use of the escape character for strings
containing escaped double quotes or backslashes, but this option changes these
to verbatim strings.
- Assume 'import java.util.*' for snippet conversions:
By default, the converter assumes that the original Java code has an 'import java.util.*'
statement for code snippet conversions, unless you uncheck this option.
- Assume 'import java.util.concurrent*' for snippet conversions:
By default, the converter assumes that the original Java code has an 'import java.util.concurrent.*'
statement for code snippet conversions, unless you uncheck this option.
- Assume 'import java.util.function.*' for snippet conversions:
By default, the converter assumes that the original Java code has an 'import java.util.function.*'
statement for code snippet conversions, unless you uncheck this option.
- Assume 'import java.util.stream.*' for snippet conversions:
By default, the converter assumes that the original Java code has an 'import java.util.stream.*'
statement for code snippet conversions, unless you uncheck this option.
- Allow C# extended interfaces (fields and default, static, & private interface methods):
By default, the converter moves Java interface fields to a separate class and comments out
Java 8 default, static, and private interface methods, but if you are targeting a newer version
of C#, which allows these features, then check this option.
- Convert Java 'final' parameters to C# 'in' parameters
If you select this option, Java final parameters will convert to C# in parameters.
- Place 'using' statements converted from existing 'import' statements at the top of files
By default, the converter leaves using statements converted from Java import statements in their original
location, but this option forces all using statements to the top of each file.
- Use target-typed new in field and local variable declarations:
If you choose this option, the converter will convert local variable and field declarations to use
'target-typed new' syntax (e.g., Foo<Bar> fooBar = new Foo<Bar>() becomes Foo<Bar> fooBar = new()).
- Default namespace if package statement is missing
If you specify a default namespace, then it will be added
to files which are missing the package statement.
- Omit conversion of 'import' statements starting with:
By default, the converter omits the conversion of 'import' statements for package names
beginning with 'java' and 'javax', but you can add to these or remove them.
Copyright © Tangible Software Solutions, Inc.